package com.zhang.demo.ui.adaper;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import com.zhang.demo.R;

import java.util.List;

/**
 * Created by zhangke on 16/10/19.
 */
public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener,
        View.OnLongClickListener {
    protected final String myTag = getClass().getSimpleName();
    protected T data;
    @IdRes
    public static final int ITEM_ID = R.id.recycler_item_view_id;

    private OnItemClickListener itemClickListener;
    private OnItemLongClickListener itemLongClickListener;

    public BaseViewHolder(Context context, int layoutResId, ViewGroup parent) {
        this(createView(context, parent, layoutResId));
    }

    public BaseViewHolder(View itemView) {
        super(itemView);
        preConfig();
        if (itemView.getId() == View.NO_ID) {
            itemView.setId(ITEM_ID);
        }
        initViews();
        initListeners();
    }
    protected void preConfig() {
    }

    protected void initViews() {

    }
    protected void initListeners() {
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    public final void onBind(T data) {
        onBind(data, 0);
    }

    public final void onBind(T data, int position) {
        this.data = data;
        display(data, position);
    }

    public void onPayloadsChanged(@NonNull List<Object> payloads) {

    }

    protected void display(T data, int position) {
        display(data);
    }

    protected abstract void display(@NonNull T data);

    public T getData() {
        return data;
    }

    public void setItemClickListener(OnItemClickListener listener) {
        itemClickListener = listener;
    }

    public void setItemLongClickListener(OnItemLongClickListener longClickListener) {
        itemLongClickListener = longClickListener;
    }

    @Override
    public void onClick(View v) {
        if (itemClickListener != null) {
            int position = getAdapterPosition();
            if (position >= 0 ) {
                itemClickListener.onItemClick(this, v, position);
            } else if (v.getId() != ITEM_ID) {
                itemClickListener.onItemClick(this, v, position);
            } else {
                Log.i(myTag,"onClick position NO_POSITION");
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (itemLongClickListener != null) {
            int position = getAdapterPosition();
            return itemLongClickListener.onItemLongClick(this, v, position);
        }
        return false;
    }

    protected static View createView(Context context, ViewGroup parent, int layoutResId) {
        return LayoutInflater.from(context).inflate(layoutResId, parent, false);
    }

    public Context getContext() {
        return itemView.getContext();
    }

    public <V extends View> V findViewById(@IdRes int id) {
        return itemView.findViewById(id);
    }

    public String getString(@StringRes int id, Object... args) {
        return getContext().getString(id, args);
    }

    public Resources getResources() {
        return getContext().getResources();
    }


    @Override
    public String toString() {
        return " " + myTag + ":" + data + ":" + super.toString();
    }
}
